import subprocess
import json
import sys

def get_media_info(file_path):
    """获取视频文件的编码信息"""
    command = [
        'ffprobe',
        '-v', 'quiet',
        '-print_format', 'json',
        '-show_streams',
        file_path
    ]
    
    try:
        # 运行ffprobe命令
        result = subprocess.run(command, capture_output=True, text=True, check=True)
        data = json.loads(result.stdout)
        
        video_codec = None
        audio_codec = None
        
        # 解析视频流和音频流信息
        for stream in data.get('streams', []):
            if stream['codec_type'] == 'video' and video_codec is None:
                video_codec = stream.get('codec_name')
            elif stream['codec_type'] == 'audio' and audio_codec is None:
                audio_codec = stream.get('codec_name')
        
        return {
            'video_codec': video_codec or '未找到',
            'audio_codec': audio_codec or '未找到'
        }
        
    except Exception as e:
        print(f"错误: {str(e)}")
        return None

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("用法: python media_info.py <视频文件路径>")
        sys.exit(1)
    
    file_path = sys.argv[1]
    info = get_media_info(file_path)
    
    if info:
        print(f"视频编码格式: {info['video_codec']}")
        print(f"音频编码格式: {info['audio_codec']}")